package com.das.mechanic_alone.mvp.view.alone;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_alone.R;
import com.das.mechanic_alone.R2;
import com.das.mechanic_alone.mvp.contract.alone.X3AloneServiceContract;
import com.das.mechanic_alone.mvp.presenter.alone.X3AloneServicePresenter;
import com.das.mechanic_alone.mvp.view.alone.fragment.X3AloneServiceFragment;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.alone.ServiceHeaderBean;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.das.mechanic_base.utils.X3ViewPagerBind;
import com.das.mechanic_base.widget.X3BottomAlreadyAloneServiceDialog;
import com.umeng.analytics.MobclickAgent;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = X3AppRouterConstants.Router.NEW_ALONE_SERVICE)
public class X3NewAloneServiceActivity extends X3BaseActivity<X3AloneServicePresenter> implements X3AloneServiceContract.View {

    @BindView(R2.id.tb_alone)
    MagicIndicator tb_alone;
    @BindView(R2.id.vp_alone)
    ViewPager2 vp_alone;
    @BindView(R2.id.rl_loading)
    RelativeLayout rl_loading;
    @BindView(R2.id.tv_shop_num)
    TextView tv_shop_num;
    @BindView(R2.id.rl_shop)
    RelativeLayout rl_shop;
    @BindView(R2.id.tv_search)
    TextView tv_search;
    @BindView(R2.id.rl_header)
    RelativeLayout rl_header;

    private long questionOptionId;
    private HomeMainCarBean carBean;
    private long workBaseId;
    private long questionBaseId;
    private String decTitle;
    private List<ServiceHeaderBean> titleList = new ArrayList<>();
    private X3BottomAlreadyAloneServiceDialog alreadyAloneServiceDialog;
    private boolean needUpdateMile;

    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.x3_activity_new_alone_service;
    }

    @Override
    public boolean isTransparent() {
        return true;
    }

    @Override
    protected void initView() {
        if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        rl_header.setPadding(0,X3StatusBarUtil.getStatusBarHeight(this),0,0);
        X3StatusBarUtil.darkMode(this);
        carBean = (HomeMainCarBean) getIntent().getSerializableExtra("carBean");
        workBaseId = getIntent().getLongExtra("workBaseId", 0L);
        questionBaseId = getIntent().getLongExtra("questionBaseId", 0L);
        decTitle = getIntent().getStringExtra("decTitle");
        needUpdateMile = getIntent().getBooleanExtra("needUpdateMile", false);
        questionOptionId = getIntent().getLongExtra("questionOptionId", 0L);
        Map<String,Object> map = new HashMap<>();
        map.put("workBaseId", workBaseId);
        map.put("questionBaseId",questionBaseId);
        map.put("questionOptionId",questionOptionId);
        initTabLayout();
        if (mPresenter != null) {
            rl_loading.setVisibility(View.VISIBLE);
            mPresenter.getServiceBaseInfo(map,false);
        }

    }

    private void initTabLayout() {
        CommonNavigator tbNavigator = new CommonNavigator(this);
        tbNavigator.setScrollPivotX(0.65f);
        tbNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return X3StringUtils.isListEmpty(titleList) ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView titleView = new SimplePagerTitleView(context);
                titleView.setText(titleList.get(index).getServiceCategoryValue());
                titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                titleView.setTypeface(ResourcesCompat.getFont(context, com.das.mechanic_base.R.font.pingfan_bold));
                titleView.setNormalColor(Color.parseColor("#b1b3bd"));
                titleView.setSelectedColor(Color.parseColor("#333333"));
                titleView.setOnClickListener(v -> vp_alone.setCurrentItem(index));
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(X3ScreenUtils.dipToPx(3,context));
                indicator.setLineWidth(X3ScreenUtils.dipToPx(16,context));
                indicator.setRoundRadius(X3ScreenUtils.dipToPx(3,context));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.parseColor("#0077ff"));
                return indicator;
            }
        });
        tb_alone.setNavigator(tbNavigator);
        X3ViewPagerBind.bindViewpager2(tb_alone,vp_alone);
    }

    @Override
    protected X3AloneServicePresenter createPresenter() {
        return new X3AloneServicePresenter();
    }

    @Override
    public long providerWorkBaseId() {
        return workBaseId;
    }

    @Override
    public void getServiceInfo(List<ServiceHeaderBean> mList, boolean isJump) {
        rl_loading.setVisibility(View.GONE);
        if (X3StringUtils.isListEmpty(mList)) {
            titleList = new ArrayList<>();
        }else {
            titleList = mList;
        }
        List<X3AloneServiceFragment> fragmentList = new ArrayList<>();

        for (int i =0; i < titleList.size();i++){
            fragmentList.add(new X3AloneServiceFragment(mList.get(i).getShopCategoryEntityList(),questionBaseId,decTitle,questionOptionId));
        }
        tb_alone.getNavigator().notifyDataSetChanged();
        vp_alone.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public X3AloneServiceFragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });
        vp_alone.setOffscreenPageLimit(1);
        tb_alone.onPageSelected(0);
    }

    @Override
    public void getServiceList(List<AloneServiceListBean> mList, boolean isStart) {

    }


    @Override
    public void getUpdateServiceList(List<AloneServiceListBean> mList) {

    }

    @Override
    public void searchAloneCache() {

    }

    @Override
    public void listServiceBaseByCategorySus(List<ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean> list) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("检测系统添加服务项目弹窗");


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetPictureOrVideo(CameraBean cameraBean) {
        if (cameraBean == null) {
            return;
        }
        switch ((int) cameraBean.resultCode) {
            case X3Constent.CODE_ALONE_ADD_SERVICE_REQUEST:
                if (cameraBean.cameraData == null) {
                    return;
                }
                List<AloneServiceListBean> serviceList = (List<AloneServiceListBean>) cameraBean.cameraData;
                tv_shop_num.setVisibility(X3StringUtils.isListEmpty(serviceList) ? View.GONE : View.VISIBLE);
                break;
            case X3Constent.CODE_ALONE_ADD_SERVICE_REFRESH:
                Map<String,Object> map = new HashMap<>();
                map.put("workBaseId", workBaseId);
                map.put("questionBaseId",questionBaseId);
                map.put("questionOptionId",questionOptionId);
                if (mPresenter != null) {
                    rl_loading.setVisibility(View.VISIBLE);
                    mPresenter.getServiceBaseInfo(map,false);
                }
                break;
        }         
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R2.id.iv_back,R2.id.rl_shop,R2.id.tv_search})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
            overridePendingTransition(0, R.anim.activity_exit);
        } else if (id == R.id.rl_shop) {
            if (alreadyAloneServiceDialog == null) {
                alreadyAloneServiceDialog = new X3BottomAlreadyAloneServiceDialog(this);
            }
            alreadyAloneServiceDialog.show();
            alreadyAloneServiceDialog.getCurrentDetectionList(providerWorkBaseId(), needUpdateMile, carBean);
        } else if (id == R.id.tv_search) {
            if (!X3Utils.isFastClick()) {
                return;
            }
            /**
             * 跳转到模糊搜索服务项目
             */
            Bundle bundle = new Bundle();
            bundle.putLong("workBaseId",providerWorkBaseId());
            bundle.putLong("questionBaseId", questionBaseId);
            bundle.putString("decTitle", decTitle);
            bundle.putBoolean("needUpdateMile", needUpdateMile);
            bundle.putLong("questionOptionId", questionOptionId);
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.ALONE_SEARCH,bundle);
        }
    }
}